<?php
declare (strict_types=1);

namespace app\api\model\boss;

use app\api\service\boss\MBaseService;
use app\common\enum\order\OrderStatus as OrderStatusEnum;
use app\common\enum\order\PayStatus as OrderPayStatusEnum;
use think\facade\Db;

class MCurveV2 extends MBaseService
{
    public function income($selectParams)
    {
        $payTypeSql = ' 1 = 1 ';
        $total_pay_price = ' g.total_pay_price ';
        $total_price = ' g.total_price ';
        switch ($selectParams['payType']) {
            case 1: // 现金支付方式
                $payTypeSql = ' realPayType = 20 ';
                break;
            case 2: // 三券支付方式
                $payTypeSql = ' realPayType in (7,6,5) ';
                $total_pay_price = ' g.deduction_value ';
                $total_price = ' g.deduction_value as total_price ';
                break;
            case 4: // 充值支付方式
                $payTypeSql = ' realPayType = 10 ';
                break;
        }

        $sql = '';
        switch ($selectParams['curveType']) {
            case 1: // 营收额
                switch ($selectParams['incomeType']) {
                    case 1: // 标价
                        $sql = 'sum(total) as saleMoney';
                        break;
                    case 2: // 业绩
                        $sql = 'sum(pay_price) as saleMoney';
                        break;
                    case 3: // 实际
                        $sql = 'sum(realIncome) as saleMoney';
                        break;
                }
                break;
            case 2: // 毛利额
                switch ($selectParams['incomeType']) {
                    case 1: // 标价
                        $sql = 'sum(total) - sum(cost_money) as saleMoney';
                        break;
                    case 2: // 业绩
                        $sql = 'sum(pay_price) - sum(cost_money) as saleMoney';
                        break;
                    case 3: // 实际
                        $sql = 'sum(realIncome) - sum(cost_money) as saleMoney';
                        break;
                }
                break;
            case 3: // 毛利率
                switch ($selectParams['incomeType']) {
                    case 1: // 标价
                        $sql = 'sum(total) - sum(cost_money) / sum(total) * 100 as saleMoney';
                        break;
                    case 2: // 业绩
                        $sql = 'sum(pay_price) - sum(cost_money) / sum(pay_price) * 100 as saleMoney';
                        break;
                    case 3: // 实际
                        $sql = 'sum(realIncome) - sum(cost_money) / sum(realIncome) * 100 as saleMoney';
                        break;
                }
                break;
            case 4:
                $sql = 'count(*) as saleMoney';
                break;
        }

        $sql = '
select ' . $sql . ', FROM_UNIXTIME(create_time, "' . MBaseService::sqlUnixFormat($selectParams) . '" ) as addtime from (	
select *,(sum(total_price)- sum(refund_total_price)) as total,sum(total_pay_price) as pay_price,sum(total_num) as goodsNum,sum(cost_price) as cost_money,(pay_money+bd_income) as realIncome from (
select r.refund_money,if((r.type = 10 or r.type = 30) and r.audit_status=10 and r.status=20,0,g.total_num) as total_num,if((r.type = 10 or r.type = 30) and r.audit_status=10 and r.status=20,0,g.cost_price) as cost_price,if((r.type = 10 or r.type = 30) and r.audit_status=10 and r.status=20,r.refund_money,0) as refund_total_price,if((r.type = 10 or r.type = 30) and r.audit_status=10 and r.status=20,g.total_pay_price-r.refund_money,'.$total_pay_price.') as total_pay_price,'.$total_price.',o.create_time,o.order_id,o.order_parent_id,s.pay_money,case s.bd_type when 7 then round(s.deduction_value * 0.5,2) when 6 then 0 when 5 then 0 when 10 then round(s.deduction_value,2) else s.deduction_value end  as bd_income,if(s.bd_type =  0,s.pay_type,s.bd_type) as realPayType 
from yoshop_order_goods g
left join yoshop_order o on o.order_id = g.order_id
left join yoshop_order_sub s on s.order_id = g.order_parent_id
left join yoshop_order_refund r on r.order_id = g.order_id and r.order_goods_id = g.order_goods_id
where o.create_time >= ' . $selectParams['startTime'] . ' and o.create_time <= ' . $selectParams['endTime'] . ' and o.pay_status = ' . OrderPayStatusEnum::SUCCESS . ' and (o.order_status = ' . OrderStatusEnum::COMPLETED . ' or o.order_status = ' . OrderStatusEnum::NORMAL . ' ) ) a
group by order_parent_id,realPayType ) b where ' . $payTypeSql . ' group by addtime';
        $result = Db::query($sql);
        return parent::formatCurveData($selectParams, $result);
    }
}
